package cn.yll.user.controller;


import cn.yll.common.exception.BsErrEnum;
import cn.yll.common.module.RestModel;
import cn.yll.common.utils.CopierUtil;
import cn.yll.api.util.RestUtil;
import cn.yll.api.user.request.UserExtendModifyRequest;
import cn.yll.api.user.response.UserExtendResponse;
import cn.yll.user.entity.UserExtendEntity;
import cn.yll.user.service.IUserExtendService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;

import java.util.Objects;


/**
 * <p>
 * 用户扩展信息表 前端控制器
 * </p>
 *
 * @author yll
 * @since 2022-08-01
 */
@RestController
@Api(tags = "用户扩展信息表相关接口")
@RequestMapping("/userExtend")
public class UserExtendController extends BaseController {
    @Autowired
    private IUserExtendService userExtendService;

    @PostMapping("/modify/userId")
    @ApiOperation(value = "更新用户扩展信息接口", notes = "用户ID必填")
    public RestModel<Boolean> modifyByUserId(@RequestBody UserExtendModifyRequest modifyRequest) {
        if (Objects.isNull(modifyRequest) || Objects.isNull(modifyRequest.getUserId())) {
            return RestUtil.error(BsErrEnum.PARAM_EMPTY);
        }
        UserExtendEntity userEntity = userExtendService.lambdaQuery().eq(UserExtendEntity::getUserId, modifyRequest.getUserId()).one();
        if (Objects.isNull(userEntity)) {
            return RestUtil.error("用户ID不存在");
        }
        userEntity = CopierUtil.convertT(modifyRequest, UserExtendEntity.class);
        assert userEntity != null;
        userEntity.setUserId(null);
        LambdaUpdateWrapper<UserExtendEntity> wrapper = Wrappers.<UserExtendEntity>lambdaUpdate().eq(UserExtendEntity::getUserId, modifyRequest.getUserId());
        Boolean result = userExtendService.update(userEntity, wrapper);
        return RestUtil.sucess(result);
    }

    @GetMapping("/getById/{id}")
    @ApiOperation(value = "根据ID查询用户扩展信息", notes = "主键ID必填")
    public RestModel<UserExtendResponse> getUserExtendById(@PathVariable("id") Long id) {
        if (Objects.isNull(id)) {
            return RestUtil.error(BsErrEnum.PARAM_EMPTY);
        }
        UserExtendEntity userEntity = userExtendService.getById(id);
        return RestUtil.sucess(CopierUtil.convertT(userEntity, UserExtendResponse.class));
    }

    @GetMapping("/getByUserId/{userId}")
    @ApiOperation(value = "根据用户ID查询扩展信息", notes = "userId必填")
    public RestModel<UserExtendResponse> getUserExtendByUserId(@PathVariable("userId") Long userId) {
        if (Objects.isNull(userId)) {
            return RestUtil.error(BsErrEnum.PARAM_EMPTY);
        }
        UserExtendEntity userEntity = userExtendService.lambdaQuery().eq(UserExtendEntity::getUserId, userId).one();
        return RestUtil.sucess(CopierUtil.convertT(userEntity, UserExtendResponse.class));
    }

    @GetMapping("/getByIdNo/{idNo}")
    @ApiOperation(value = "根据身份证号查询扩展信息", notes = "身份证号必填")
    public RestModel<UserExtendResponse> getByIdNo(@PathVariable("idNo") String idNo) {
        if (StringUtils.isBlank(idNo)) {
            return RestUtil.error(BsErrEnum.PARAM_EMPTY);
        }
        UserExtendEntity userEntity = userExtendService.lambdaQuery().eq(UserExtendEntity::getIdNo, idNo).one();
        return RestUtil.sucess(CopierUtil.convertT(userEntity, UserExtendResponse.class));
    }

    @PostMapping("/getByEmail")
    @ApiOperation(value = "根据邮箱账号查询扩展信息", notes = "邮箱必填")
    public RestModel<UserExtendResponse> getByEmail(@RequestBody String email) {
        if (StringUtils.isBlank(email)) {
            return RestUtil.error(BsErrEnum.PARAM_EMPTY);
        }
        UserExtendEntity userEntity = userExtendService.lambdaQuery().eq(UserExtendEntity::getEmail, email).one();
        return RestUtil.sucess(CopierUtil.convertT(userEntity, UserExtendResponse.class));
    }
}
